package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.dto.QueryTrainingDTO;

import com.woniu.entity.PageData;
import com.woniu.entity.dto.SearchTrainingDTO;
import com.woniu.entity.dto.TrainingDTO;
import com.woniu.entity.vo.TrainingVO;
import com.woniu.mapper.CollectMapper;
import com.woniu.pojo.Training;
import com.woniu.mapper.TrainingMapper;
import com.woniu.service.ITrainingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-03-31
 */
@Service
public class TrainingServiceImpl extends ServiceImpl<TrainingMapper, Training> implements ITrainingService {
    @Autowired
    private TrainingMapper trainingMapper;
    @Resource
    private CollectMapper collectMapper;
//    分页查询靶场
    @Override
    public PageData listSearch(Integer currentPage, Integer pageSize, QueryTrainingDTO queryTrainingDTO) {
        //生成wapper,并对传回的Dto对象进行校验
        QueryWrapper<Training> wrapper = new QueryWrapper();
        if(queryTrainingDTO != null){
            if(!StringUtils.isEmpty(queryTrainingDTO.getTrainingName())){
                wrapper.like("training_name", queryTrainingDTO.getTrainingName());
            }
            if(queryTrainingDTO.getDifficulty() != null){
                wrapper.eq("difficulty", queryTrainingDTO.getDifficulty());
            }
            if(queryTrainingDTO.getPrice() != null){
                wrapper.eq("price", queryTrainingDTO.getPrice() );
            }
            if(queryTrainingDTO.getIsVip() != null){
                wrapper.eq("is_vip", queryTrainingDTO.getIsVip() );
            }
            if(queryTrainingDTO.getCreateTimeStart() != null){
                wrapper.gt("create_time", queryTrainingDTO.getCreateTimeStart());
            }
            if(queryTrainingDTO.getCreateTimeEnd() != null){
                wrapper.lt("create_time", queryTrainingDTO.getCreateTimeEnd());
            }
        }
        // 构建分页对象
        IPage<Training> page = new Page<>(currentPage, pageSize);
        // 查询
        IPage<Training> trainingIPage = trainingMapper.selectPage(page, wrapper);
        // 返回数据
        return new PageData(trainingIPage.getTotal(), trainingIPage.getRecords());
    }

    @Override
    public Boolean nameIsExist(String name) {
        QueryWrapper<Training> wrapper = new QueryWrapper<>();
        wrapper.eq("training_name", name);
        Training training = trainingMapper.selectOne(wrapper);
        if (StringUtils.isEmpty(training)) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public Boolean checkCollect(Integer uid, String title) {

        Integer iscollect = trainingMapper.checkCollect(uid, title);
        if (iscollect !=null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Integer getParentId(String pName) {
        Integer parentId = trainingMapper.getParentId(pName);
        return parentId;
    }

    @Override
    public Boolean switchVIP(Integer id, Integer isVip) {
        Boolean flag= trainingMapper.switchVIP(id,isVip);
        return flag;
    }



    @Override
    public List<TrainingVO> showFollow(Integer uid) {
        List<TrainingVO> follows=trainingMapper.showFollow(uid);
        return follows;
    }

    @Override
    public Set<TrainingVO> recommend() {
        Set<TrainingVO> recommends=trainingMapper.showRecommend();
        return recommends;
    }

    @Override
    public PageData searchBar(Integer currentPage, Integer pageSize, Integer userId,String title, SearchTrainingDTO searchTrainingDTO) {
//        把前端传递的路径参数存到DTO中，赋初始值
        searchTrainingDTO.setTrainingName(title);
        searchTrainingDTO.setUserId(userId);
        if (searchTrainingDTO.getTrainingParentId()==""){
            searchTrainingDTO.setTrainingParentId(null);
        }


        // 构建分页对象
        IPage<TrainingVO> page = new Page<>(currentPage, pageSize);
        // 查询
        IPage<TrainingVO> trainingIPage = trainingMapper.searchBar(page, searchTrainingDTO);
        // 返回数据
        return new PageData(trainingIPage.getTotal(), trainingIPage.getRecords());
    }

    @Override
    public ArrayList<String> showParent() {
        ArrayList<String> parents = trainingMapper.showParent();
        return parents;
    }

//    靶场内部查询方式


    @Override
    public PageData search(Integer currentPage, Integer pageSize, TrainingDTO trainingDto) {
        IPage<TrainingVO> page = new Page<>(currentPage,pageSize);
        List<TrainingVO> voIPage =trainingMapper.selectAll(trainingDto);
        page.setRecords(voIPage);

        PageData pageData1 = new PageData(page.getTotal(),page.getRecords());

        return pageData1;
    }
}
